﻿using POC.Configuration;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace POC.Configuration
{
    public class ConfigConst
    {
        public  const string filePath = "filePath";
        public  const string nodeName = "nodeName";
        public  const string useAppsettings = "useAppsettings";
        public  const string connectionStringName = "connectionStringName";
        public  const string queueName = "queueName";
        public  const string tableName = "tableName";
        public  const string account = "account";
        public  const string accessKey = "accessKey";
        public  const string isHttps = "isHttps";
        public  const string usePlainText = "usePlainText";
        public  const string async = "async";
        public  const string onlyTrace = "onlyTrace";
        public const string rollingDateFormat = "rollingDateFormat";
        public const string useLocalAccount = "useLocalAccount";
        public const string messageFormat = "messageFormat";
        

        public static string[] GetSupportedTypes()
        {
            return new string[] { filePath, 
                nodeName, useAppsettings ,  
                connectionStringName,
                queueName,tableName,account,
                accessKey,isHttps,usePlainText,
                async, onlyTrace, rollingDateFormat,
                useLocalAccount, messageFormat
            };
        }


    }
    public class AzureAccountSettings
    {
        internal StringDictionary dictionary;
        public bool Async { get; set; }
        public string DefaultStorage { get; set; }
        public string GetRollingName(string storageType)
        {
           if(storageType == "table")
               return  string.Format("{0}table{1}", tableName, EntitySuffix);
            else
              return  string.Format("{0}queue{1}", queueName, EntitySuffix); 
        }
        public string EntitySuffix
        {
            get
            {
                if (string.IsNullOrEmpty(this.rollingDateFormat))
                {
                    return "";
                }
                else
                {
                    return DateTime.Now.ToString(this.rollingDateFormat);
                }
            }
        }

       

        public bool useLocalAccount
        {
            get
            {
                string tf = dictionary[ConfigConst.useLocalAccount];
                return tf.ToBoolean();
            }
        }
        public string messageFormat
        {
            get
            {
                return dictionary[ConfigConst.messageFormat];
            }
        }
        public string rollingDateFormat
        {
            get
            {
                return dictionary[ConfigConst.rollingDateFormat];
            }
        }
        public string filePath
        {
            get
            {
                return dictionary[ConfigConst.filePath];
            }
        }
        public  string nodeName
        {
            get
            {
                return dictionary[ConfigConst.nodeName];
            }
        }
        public bool useAppsettings
        {
            get
            {
                string use = dictionary[ConfigConst.useAppsettings];
                return use.ToBoolean();
            }
        }
        public string connectionStringName
        {
            get
            {
                return dictionary[ConfigConst.connectionStringName];
            }
        }
        public string queueName
        {
            get
            {
                return dictionary[ConfigConst.queueName];
            }
        }
        public string tableName
        {
            get
            {
                return dictionary[ConfigConst.tableName];
            }
        }
        public string account
        {
            get
            {
                return dictionary[ConfigConst.account];
            }
        }
        public string accessKey
        {
            get
            {
                return dictionary[ConfigConst.accessKey];
            }
        }
        public bool isHttps
        {
            get
            {

                string https = dictionary[ConfigConst.isHttps];
                return https.ToBoolean();
            }
        }
        public bool usePlainText
        {
            get
            {
                string use = dictionary[ConfigConst.usePlainText];
                return use.ToBoolean();
            }
        }
        public bool async
        {
            get
            {
                string use = dictionary[ConfigConst.async];
                return use.ToBoolean();
            }
        }
        public bool onlyTrace
        {
            get
            {
                string use = dictionary[ConfigConst.onlyTrace];
                return use.ToBoolean();
            }
        }


        public AzureAccountSettings()
        {
            Async = false;
            DefaultStorage = "table";
            
        }      

        public void Add(string key, string value)
        {
            if (dictionary == null) dictionary = new StringDictionary();
            if (dictionary.ContainsKey(key))
            {
                // dictionary[key] = value;
                // dont add
            }
            else
            {
                dictionary.Add(key.ToLower(), value);
            }
        }
        public void Parse(string initializeData)
        {
            dictionary = new StringDictionary();
            string[] initData = initializeData.Split(',', ';');

            foreach (string item in initData)
            {
                string[] data = item.Split(new char[] { '=' }, 2);
                if (data.Length != 2)
                    continue;
                dictionary.Add(data[0].ToLower(), data[1]);
            }
        }

        public void Parse(string filePath, string nodeName)
        {
            dictionary = new StringDictionary();
            FileConfigReader ireader = new FileConfigReader(filePath, nodeName);
            foreach (var item in ireader.List(nodeName))
            {
                dictionary.Add(item.Key.ToLower(), item.Value);
            }
        }
    }
}
